package org.fuys.etp.repo.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.fuys.etp.core.model.vo.TpConfigHistoryVO;
import org.fuys.etp.repo.converter.PoToVoConverter;
import org.fuys.etp.repo.converter.VoToPoConverter;
import org.fuys.etp.repo.mapper.EtpConfigHistoryMapper;
import org.fuys.etp.repo.po.EtpConfigHistory;
import org.fuys.etp.repo.repository.IEtpUpdateHistoryRepository;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-etp
 * @package: org.fuys.etp.repo.repository.impl
 * @className: EtpUpdateHistoryRepository
 * @author: WangJing
 * @description:
 * @date: 2025/2/6 23:38
 * @version: 1.0
 */
@Component
public class EtpUpdateHistoryRepository implements IEtpUpdateHistoryRepository {

    @Resource
    private EtpConfigHistoryMapper etpConfigHistoryMapper;

    @Override
    public void insertHistoryRecord(TpConfigHistoryVO history) {
        final EtpConfigHistory configHistory = VoToPoConverter.converter(history);
        etpConfigHistoryMapper.insert(configHistory);
    }

    @Override
    public List<TpConfigHistoryVO> selectHistoryRecord(String type, String poolName, Integer count) {
        final Page<EtpConfigHistory> etpConfigHistoryPage = etpConfigHistoryMapper.selectPage(new Page<>(1, count), new LambdaQueryWrapper<EtpConfigHistory>()
                .eq(EtpConfigHistory::getPoolName, poolName).eq(EtpConfigHistory::getPoolType, type));
        if(etpConfigHistoryPage == null || etpConfigHistoryPage.getRecords() == null){
            return Collections.emptyList();
        }
        return etpConfigHistoryPage.
                getRecords().stream().map(PoToVoConverter::convert).collect(Collectors.toList());
    }
}
